#include"DynamicArray.h"





struct DynamicArray
{
     ElementType *array;//数组指针
     int capacity;//数组容量
     int size;//元素个数
};

DArray *InitDArray()
{   
    DArray *a=(DArray*)malloc(sizeof(DArray));
    if(a==NULL)
    {
        printf("InitDArray malloc error!\n");
        exit(-1);//退出程序
    }
    a->size=0;
    a->capacity=10;
    a->array=(ElementType *)malloc(sizeof(ElementType )*a->capacity);
    if(a->array==NULL)
    {
         printf("InitDArray  Array  malloc error!\n");
        exit(-1);//退出程序
    }


    return a;
}

ElementType FindByElement(DArray *a, ElementType value,  bool (*compare)(ElementType, ElementType))
{
    for(int i=0;i<a->size;i++)
    {
        if(compare(a->array[i],value))
            return a->array[i];
    }
    return NULL;
}
ElementType DeleteByIndex(DArray*a,int index)
{
    ElementType result=a->array[index];
    for(int i=index;i<a->size-1;i++)
    {
        a->array[i]=a->array[i+1];

    }
    a->size--;
    return result;
}
void MyFastSort(ElementType *a,int start,int end,bool(*compare)(ElementType, ElementType))
{
  //终止条件
    if(start>=end)
        return;
    int left=start;
    int right=end;
    ElementType temp=a[start];
    while(left<right)
    {
        //从右往左找基准值小
        while(compare(a[right],temp)&&right>left)
            right--;
        if(left<right)
        {
            a[left++]=a[right];//把比基准值小的数放到左边，在向右++
        }
        while(compare(temp,a[left])&&left<right)
            left++;
        if(left<right)
            a[right--]=a[left];//把比基准值大的放到右边，在向左--
    }
    a[left]=temp;
    MyFastSort(a,start,left-1,compare);
    MyFastSort(a,right+1,end,compare);
  
}
void FastSort(DArray *a,bool(*compare)(ElementType, ElementType))
{
    MyFastSort(a->array,0,a->size-1,compare);
}
ElementType DeleteElement(DArray *a, ElementType value, bool(*compare)(ElementType, ElementType))
{
      
        for(int i=0;i<a->size;i++)
        {
            if(compare(a->array[i],value))
            {
                return DeleteByIndex(a,i);
            }   
        }
        return NULL;
}
int Namecmp(void *p1,void *p2)
{
   
     Stu *s1=(Stu *)p1;
     return strcmp(s1->name,(const char*)p2);
}
void ClearDArray(DArray *a,void(*clear)(ElementType))
{
    if(a!=NULL)
    {
        for(int i=0;i<a->size;i++)
        {
            clear(a->array[i]);
        }
    free(a->array);
    free(a);
    }
}




void InsertDArray(DArray *a, ElementType element)
{   
    if(a->size==a->capacity)
    {
        a->capacity*=2;
        a->array=(ElementType*)realloc(a->array,a->capacity*sizeof(ElementType)); 
        if(a->array==NULL)
        {
            printf("InsertDArray ralloc error!\n");
            exit(-1);//退出程序
        }
    }
    a->array[a->size++]=element;
}
//回调函数
void PrintDArray(DArray *a,void(*print)(ElementType))
{
    printf("capacity :%d size:%d \n",a->capacity,a->size);
    for(int i=0;i<a->size;i++)
    {
        print(a->array[i]);//函数指针存放printstu函数指针
       
    }
     printf("\n");
}
